package com.example.listener;

import com.example.enums.Events;
import com.example.enums.Status;
import com.example.po.SysSms;
import com.example.service.SysSmsServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;


/****
 *
 * 状态机的监听程序
 */
public class SmsStateMachineListener implements StateMachineListener<Status, Events> {

    Logger logger = LoggerFactory.getLogger(getClass());

    private StateContext<Status, Events> stateContext;

    //状态改变的时候触发
    @Override
    public void stateChanged(State<Status, Events> state, State<Status, Events> state1) {

    }

    //状态改变的时候触发
    @Override
    public void stateEntered(State<Status, Events> state) {

    }

    //状态改变结束的时候触发
    @Override
    public void stateExited(State<Status, Events> state) {

    }

    @Override
    public void eventNotAccepted(Message<Events> message) {
        logger.warn("状态机 eventNotAccepted" + message);
    }

    // 在状态机的状态转换时调用
    @Override
    public void transition(Transition<Status, Events> transition) {
       /* Message<Events> message = this.stateContext.getMessage();
        if (message != null) {
            SysSms sysSms = (SysSms) message.getHeaders().get(SysSmsServiceImpl.SMS);
            //申请发送成功触发事件
            if (transition.getTarget().getId() == Status.READY) {
                logger.warn("申请发送成功触发事件");
            }

            //发送失败触发事件
            if (transition.getTarget().getId() == Status.FAIL) {
                logger.warn("短信id：" + sysSms.getId() + " 触发了发送失败事件");
            }

            //发送成功触发事件
            if (transition.getTarget().getId() == Status.SUCCESS) {
                logger.warn("短信id：" + sysSms.getId() + " 触发了发送成功事件");
            }

            //确认发送成功触发事件
            if (transition.getTarget().getId() == Status.COMMIT) {
                logger.warn("短信id：" + sysSms.getId() + " 触发了确认发送成功事件");
            }
        }*/
    }

    // 在状态机开始进行状态转换时调用
    @Override
    public void transitionStarted(Transition<Status, Events> transition) {

    }

    // 在状态机进行状态转换结束时调用
    @Override
    public void transitionEnded(Transition<Status, Events> transition) {

    }

    @Override
    public void stateMachineStarted(StateMachine<Status, Events> stateMachine) {
    }

    @Override
    public void stateMachineStopped(StateMachine<Status, Events> stateMachine) {
    }

    @Override
    public void stateMachineError(StateMachine<Status, Events> stateMachine, Exception e) {
        logger.warn("stateMachineError: " + e.getMessage());
    }

    @Override
    public void extendedStateChanged(Object o, Object o1) {

    }

    @Override
    public void stateContext(StateContext<Status, Events> stateContext) {
        this.stateContext = stateContext;
    }
}
